//
//  SAAllocateSelectShopVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2022/5/6.
//

#import "SAAllocateSelectShopVC.h"
#import "ScanHelper.h"
#import "SASelectShopCell.h"
#import "SAWarehousePurchaseFindProductPageAPI.h"
#import "SAProductHomeScreeningView.h"
#import "SASalesOrderViewListModel.h"
#import "YBImageBrowser.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SACommodityAddShopSizeView.h"
#import "GFAlertView.h"
#import "SAShoppingCartView.h"

@interface SAAllocateSelectShopVC ()<UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate,HTTPAPIDelegate,YBImageBrowserDataSource>

@property (nonatomic, strong) UITextField *searchTf;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *scanBtn;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *tipView;
@property (nonatomic, strong) SAWarehousePurchaseFindProductPageAPI *warehousePurchaseFindProductPageAPI;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) NSMutableArray<SASalesOrderViewListModel *> *dataMarr;
@property (nonatomic, strong) NSMutableArray *screenArray;
@property (nonatomic, strong) NSString *providerId;
@property (nonatomic, strong) NSString *providerName;
@property (nonatomic, strong) NSArray *imageList;
@property (nonatomic, strong) UIButton *nextBtn;
//@property (nonatomic, strong) UIButton *numBtn;
@property (nonatomic, strong) UILabel *numLab;
@property (nonatomic, strong) NSMutableArray<SASalesOrderViewListModel *> *selectMarr;
@property (nonatomic, strong) NSString *scanStr;
@property (nonatomic, strong) UIButton *cartBtn;
@property (nonatomic, strong) UILabel *carLab;
@property (nonatomic, assign) BOOL isSingle;

@end

@implementation SAAllocateSelectShopVC

-(instancetype)initWithSelectMarr:(NSMutableArray<SASalesOrderViewListModel *> *)selectMarr {
    if (self = [super init]) {
        if (selectMarr.count > 0) {
            self.selectMarr = selectMarr;
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    if (self.warehousePurchaseFindProductPageAPI == api) {
        if (self.currentPage == 1) {
            [self.dataMarr removeAllObjects];
        }
        for (NSDictionary *dict in (NSArray *)data[@"data"][@"records"]) {
            SASalesOrderViewListModel *model = [SASalesOrderViewListModel mj_objectWithKeyValues:dict];
            model.retailPrice = model.entryPrice;
            for (SASalesOrderToPendSkuList *list in model.toPendSkuAttrs) {
                list.stock = @"0";
                list.inventoryCount = list.count;
                list.price = model.entryPrice;
            }
            [self.dataMarr addObject:model];
        }
        if (self.selectMarr.count > 0) {
            for (int i = 0; i < self.dataMarr.count; i++) {
                for (SASalesOrderViewListModel *selectModel in self.selectMarr) {
                    if ([selectModel.productId isEqualToString:self.dataMarr[i].productId]) {
                        self.dataMarr[i] = selectModel;
                    }
                }
            }
        }

        [self refreshData];
        if (self.dataMarr.count > 0) {
            self.tipView.hidden = YES;
        }else{
            self.tipView.hidden = NO;
        }
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - UITextField
- (void)textFieldEditChanged:(UITextField *)textField {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchProduct:) object:textField];
    [self performSelector:@selector(searchProduct:) withObject:textField afterDelay:0.3];
}

- (void)searchProduct:(UITextField *)textField {
    self.currentPage = 1;
    self.warehousePurchaseFindProductPageAPI.currentPage = 1;
    self.warehousePurchaseFindProductPageAPI.productCodeOrBar = textField.text;
    [self.warehousePurchaseFindProductPageAPI sendRequestToServerWithActivityViewAndMask:GET];
}

#pragma mark - event
-(void)clickedScanBtn {
    [self.navigationController pushViewController:[[ScanHelper shareInstance] ScanVCWithStyle:notSquare qrResultCallBack:^(id result) {
        self.searchTf.text = result;
        self.isSingle = YES;
        self.currentPage = 1;
        self.warehousePurchaseFindProductPageAPI.productCodeOrBar = result;
        [self.warehousePurchaseFindProductPageAPI sendRequestToServerWithActivityViewAndMask:GET];
        [self.navigationController popViewControllerAnimated:NO];
    }] animated:YES];
}

-(void)backToLastVC {
    if (self.selectMarr.count > 0) {
        GFAlertView *alertView = [GFAlertView alertViewWithMsg:@"提示" detailMsg:@"所选商品暂未保存,您确定要返回吗?" knowBtnBlock:^(UIButton *knowBtn) {
            [knowBtn addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside];
        }];
        [alertView showWithAnimation];
    }else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)clickedCartBtn {
    if (self.selectMarr.count > 0) {
        for (SASalesOrderViewListModel *model in self.selectMarr) {
            model.isWarehousing = YES;
        }
        SAShoppingCartView *cartView = [[SAShoppingCartView alloc] initWith:self.selectMarr];
        WeakSelf
        cartView.refreshBlock = ^{
            [weakSelf refreshData];
        };
        cartView.refreshMarrBlock = ^(NSMutableArray * _Nonnull marr) {
            weakSelf.selectMarr = marr;
            [weakSelf refreshData];
        };
        [self.view addSubview:cartView];
    }
}

-(void)popVC {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)clickedNextBtn {
    for (SASalesOrderViewListModel *model in self.selectMarr) {
        if (model.percentage == -1) {
            model.percentage = [model.discountRate floatValue]/10000; //如果是传进来的数据，这个参数不是-1，将不改动之前的百分比
            model.salePrice = [model.retailPrice floatValue] * model.percentage;
            model.priceType = @"-1";
        }
    }
    NSMutableArray<SASalesOrderViewListModel *> *marr = [NSMutableArray array];
    for (SASalesOrderViewListModel *model in self.selectMarr) {
        BOOL isZero = YES;
        for (SASalesOrderToPendSkuList *list in model.toPendSkuAttrs) {
            if ([list.stock integerValue] != 0) {
                isZero = NO;
            }
        }
        if (isZero == NO) {
            [marr addObject:model];
        }
    }
    !self.selectBlock ?: self.selectBlock(marr);
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)showImage:(NSArray *)imageList {
    self.imageList = imageList;
    if (self.imageList.count > 0) {
        NSMutableArray *imageMarr = [NSMutableArray array];
        for (NSString *url in self.imageList) {
            NSString *newUrl = [url stringByReplacingOccurrencesOfString:@"?x-oss-process=style/thumbnail" withString:@""];
            NSString *imageUrl = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,l_720",newUrl];
            [imageMarr addObject:imageUrl];
        }
        self.imageList = imageMarr;
        YBImageBrowser *browser = [YBImageBrowser new];
        browser.dataSourceArray = self.imageList;
        browser.dataSource = self;
        browser.currentPage = 0;
        [browser show];
    }else {
        [Prompt popPromptViewWithMsg:@"该商品没有图片" duration:2];
    }
}

-(void)refreshData {
    NSInteger num = 0;
    double total = 0.f;
    for (SASalesOrderViewListModel *model in self.dataMarr) {
        double entryPrice = [model.entryPrice doubleValue]/100;
        for (SASalesOrderToPendSkuList *list in model.toPendSkuAttrs) {
            num += [list.stock integerValue];
            total += [list.stock integerValue] * entryPrice;
        }
    }
    if (num > 0) {
        self.numLab.hidden = NO;
        self.carLab.hidden = NO;
        self.numLab.text = [NSString stringWithFormat:@"%zd",num];
        self.carLab.text = [NSString stringWithFormat:@"￥%0.2lf",total];
        NSString *productViewCostPrice = [NSString stringWithFormat:@"%@",[SAValueManger shareInStance].userInfo.menuList[1][@"perms"][@"productViewCostPrice"]];
        if ([productViewCostPrice isEqualToString:@"1"]) {
            self.carLab.text = [NSString stringWithFormat:@"￥%@",@"******"];
        }
    }else {
        self.numLab.hidden = YES;
        self.carLab.hidden = YES;
    }
    [self.tableView reloadData];
}

//YBImageBrowserDataSource 代理实现赋值数据
- (NSInteger)yb_numberOfCellsInImageBrowser:(YBImageBrowser *)imageBrowser {
    return self.imageList.count;
}

- (id<YBIBDataProtocol>)yb_imageBrowser:(YBImageBrowser *)imageBrowser dataForCellAtIndex:(NSInteger)index {
    NSString *urlStr = [self.imageList objectAtIndex:index];
    YBIBImageData *data = [YBIBImageData new];
    data.imageURL = [NSURL URLWithString:urlStr];
    return data;
}

-(void)loadData {
    self.warehousePurchaseFindProductPageAPI.pageSize = 20;
    self.warehousePurchaseFindProductPageAPI.currentPage = self.currentPage;
    
    self.warehousePurchaseFindProductPageAPI.productCodeOrBar = @"";
    self.warehousePurchaseFindProductPageAPI.providerId = @"";
    if (self.searchTf.text.length > 0) {
        self.warehousePurchaseFindProductPageAPI.productCodeOrBar = self.searchTf.text;
    }
    if (self.providerId.length > 0) {
        self.warehousePurchaseFindProductPageAPI.providerId = self.providerId;
    }
    
    self.warehousePurchaseFindProductPageAPI.unitId = @"";
    self.warehousePurchaseFindProductPageAPI.order = @"";
    self.warehousePurchaseFindProductPageAPI.state = @"1";
    self.warehousePurchaseFindProductPageAPI.attrIds = @"";
    if (self.merchantId.length > 0) {
        self.warehousePurchaseFindProductPageAPI.merchantId = self.merchantId;
    }
    
    if (self.screenArray.count >0) {
        NSMutableArray *colorArray = [[NSMutableArray alloc] init];
        for (SAProductHomeScreeningModel *model in self.screenArray) {
            if ([model.name isEqualToString:@"单位"]) {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.check == YES &&![list.attrName isEqualToString:@"全部"]) {
                        self.warehousePurchaseFindProductPageAPI.unitId = [NSString stringWithFormat:@"%ld",list.attrId];
                    }
                }
            }else if ([model.name isEqualToString:@"排序"]) {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.check == YES&&![list.attrName isEqualToString:@"默认"]) {
                        self.warehousePurchaseFindProductPageAPI.order = [NSString stringWithFormat:@"%ld",list.attrId];
                    }
                }
            }else if ([model.name isEqualToString:@"状态"]) {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.check == YES) {
                        if ([list.attrName isEqualToString:@"全部"]) {
                            self.warehousePurchaseFindProductPageAPI.state = @"";
                        }else {
                            self.warehousePurchaseFindProductPageAPI.state = [NSString stringWithFormat:@"%ld",list.attrId];
                        }
                    }
                }
                
            }else
            {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.check == YES&&![list.attrName isEqualToString:@"全部"]) {
                        [colorArray addObject:[NSString stringWithFormat:@"%ld",list.attrId]];
                    }
                }
                if (colorArray.count >0) {
                    self.warehousePurchaseFindProductPageAPI.attrIds = [colorArray componentsJoinedByString:@","];
                }
            }
        }
    }

    [self.warehousePurchaseFindProductPageAPI sendRequestToServerWithActivityViewAndMask:GET];
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataMarr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SASelectShopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SASelectShopCell" forIndexPath:indexPath];
    cell.allocateModel = self.dataMarr[indexPath.row];
    WeakSelf
    cell.showImageBlock = ^{
        [weakSelf showImage:weakSelf.dataMarr[indexPath.row].imgList];
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SASalesOrderViewListModel *model = self.dataMarr[indexPath.row];
    WeakSelf
    if (model.toPendSkuAttrs.count > 1) {
        if (self.isSingle == YES) {
            if (model.targetSkuList.count == 1) {
                NSString *tSkuId = model.targetSkuList[0].skuId;
                for (SASalesOrderToPendSkuList *list in model.toPendSkuAttrs) {
                    if ([tSkuId isEqualToString:list.skuId]) {
                        NSString *num = list.stock;
                        if ([num integerValue] + 1 > [list.inventoryCount integerValue] && model.isNegative == YES) {
                            [Prompt popPromptViewWithMsg:[NSString stringWithFormat:@"库存不足,当前库存%@件",list.inventoryCount] duration:2];
                            return;
                        }else {
                            list.stock = [NSString stringWithFormat:@"%zd",[list.stock integerValue] + 1];
                        }
                    }
                }
            }
            [self refreshData];
        }else {
            SACommodityAddShopSizeView *addShopSizeView = [[SACommodityAddShopSizeView alloc] initWithListModel:model];
            addShopSizeView.isAll = YES;
            addShopSizeView.isNegative = self.isNegative;
            [addShopSizeView showView:self];
            addShopSizeView.showImageBlock = ^{
                [weakSelf showImage:weakSelf.dataMarr[indexPath.row].imgList];
            };
            addShopSizeView.selectBlock = ^(SASalesOrderViewListModel * _Nonnull model) {
                [weakSelf.selectMarr removeObject:model];
                BOOL isZero = YES;
                for (SASalesOrderToPendSkuList *list in model.toPendSkuAttrs) {
                    if ([list.stock integerValue] != 0) {
                        isZero = NO;
                    }
                }
                if (isZero == NO) {
                    [weakSelf.selectMarr addObject:model];
                }
                [weakSelf refreshData];
            };
        }
    }else {
        if (model.toPendSkuAttrs.count > 0) {
            NSString *num = model.toPendSkuAttrs[0].stock;
            if (model.isNegative == YES) {
                if ([num integerValue] + 1 > [model.toPendSkuAttrs[0].inventoryCount integerValue]) {
                    [Prompt popPromptViewWithMsg:[NSString stringWithFormat:@"库存不足,当前库存%@件",model.toPendSkuAttrs[0].inventoryCount] duration:2];
                    return;
                }else {
                    model.toPendSkuAttrs[0].stock = [NSString stringWithFormat:@"%zd",[num integerValue] + 1];
                }
            }else {
                model.toPendSkuAttrs[0].stock = [NSString stringWithFormat:@"%zd",[num integerValue] + 1];
            }
        }
        [self.selectMarr removeObject:model];
        [self.selectMarr addObject:model];
        [self refreshData];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(71);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@""];
    [self setBgImage:nil];
    self.view.backgroundColor = kBgColor;
    
    [self addRightViews:@[self.topView]];
    [self.topView addSubview:self.searchTf];
    [self.topView addSubview:self.scanBtn];
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kScaleNum(50) - SafeFooterHeight, kScreenWidth, kScaleNum(50) + SafeFooterHeight)];
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
    
    UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 0, kBaseScreenWidth, 5)];
    line.backgroundColor = kBgColor;
    [footView addSubview:line];

    [footView addSubview:self.nextBtn];
    
    UIImageView *carImageV = [[UIImageView alloc] initWithFrame:SARectMake(30, 19, 23, 22)];
    carImageV.image = [UIImage imageNamed:@"sale_car"];
    [footView addSubview:carImageV];

    [footView addSubview:self.numLab];
    [self.numLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(kScaleNum(21));
        make.top.mas_equalTo(kScaleNum(6));
        make.left.mas_equalTo(kScaleNum(42));
        make.width.mas_greaterThanOrEqualTo(kScaleNum(21));
    }];
    
    [footView addSubview:self.cartBtn];
    
    UILabel *carLab = [[UILabel alloc] initWithFrame:SARectMake(70, 19, 190, 22)];
    carLab.font = kFont(14);
    carLab.textColor = kBlackColor;
    carLab.textAlignment = NSTextAlignmentLeft;
    [footView addSubview:carLab];
    self.carLab = carLab;
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.tipView];
    
    self.currentPage = 1;
    self.providerId = @"";
    self.providerName = @"";
    [self loadData];
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage = 1;
        [weakSelf loadData];
    }];
    MJRefreshAutoStateFooter *mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage ++;
        [weakSelf loadData];
    }];
    [mj_footer setTitle:@"已经到底啦~" forState:MJRefreshStateIdle];//设置闲置状态下不显示“点击或上拉加载更多”
    self.tableView.mj_footer = mj_footer;
}

#pragma mark - lazy
-(UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:SARectMake(50, 0, 325, 44)];
    }
    return _topView;
}

-(UITextField *)searchTf {
    if (!_searchTf) {
        _searchTf = [[UITextField alloc] initWithFrame:SARectMake(45, 7, 220, 30)];
        _searchTf.placeholder = @"输入商品款号/名称/条码";
        _searchTf.backgroundColor = [UIColor whiteColor];
        _searchTf.layer.cornerRadius = kScaleNum(5);
        _searchTf.layer.masksToBounds = YES;
        _searchTf.delegate = self;
        _searchTf.font = kFont(14);
        _searchTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        UIView *leftView = [[UIView alloc] initWithFrame:SARectMake(0, 0, 26, 30)];
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:SARectMake(6, 7, 14, 16)];
        imageV.image = [UIImage imageNamed:@"shop_search"];
        [leftView addSubview:imageV];
        _searchTf.leftView = leftView;
        _searchTf.leftViewMode = UITextFieldViewModeAlways;
        [_searchTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchTf;
}


-(UIButton *)scanBtn {
    if (!_scanBtn) {
        _scanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _scanBtn.frame = SARectMake(0, 0, 30, 44);
        [_scanBtn setImage:[UIImage imageNamed:@"home_scan"] forState:UIControlStateNormal];
        [_scanBtn addTarget:self action:@selector(clickedScanBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _scanBtn;
}

-(UIButton *)nextBtn {
    if (!_nextBtn) {
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _nextBtn.frame = SARectMake(kBaseScreenWidth - 105, 5, 105, 45);
        [_nextBtn setBackgroundImage:[UIImage imageNamed:@"top_image"] forState:UIControlStateNormal];
        [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        _nextBtn.titleLabel.font = kFont(15);
        [_nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_nextBtn addTarget:self action:@selector(clickedNextBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextBtn;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight) - kScaleNum(50) - SafeFooterHeight) style:UITableViewStylePlain];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.backgroundColor = kBgColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SASelectShopCell class] forCellReuseIdentifier:@"SASelectShopCell"];
    }
    return _tableView;
}

-(UIView *)tipView {
    if (!_tipView) {
        _tipView = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight))];
        _tipView.backgroundColor = [UIColor whiteColor];
        UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Shop_search2"]];
        icon.frame = SARectMake(124, 147 - StatusHeight, 138, 83);
        [_tipView addSubview:icon];
        UILabel *titleLab = [[UILabel alloc] initWithFrame:SARectMake(0, 240 - StatusHeight, kBaseScreenWidth, 30)];
        titleLab.text = @"暂无相关信息";
        titleLab.textAlignment = NSTextAlignmentCenter;
        titleLab.textColor = kGrayColor;
        [_tipView addSubview:titleLab];
    }
    return _tipView;
}

-(void)clickedScreeningBtn {
    [self prefersStatusBarHidden];
    __weak typeof(self) weakSelf = self;
    SAProductHomeScreeningView *screenView = [SAProductHomeScreeningView shareCornView];
    screenView.delegate = self;
    [screenView loadData:self.screenArray ProviderName:self.providerName ProviderId:self.providerId];
    screenView.screeningBlock = ^(NSMutableArray * _Nonnull selectedArray, NSString * _Nonnull providerName, NSString *providerId) {
        [weakSelf.screenArray removeAllObjects];
        [weakSelf.screenArray addObjectsFromArray:selectedArray];
        if (providerName.length > 0) {
            weakSelf.providerId = providerId;
            weakSelf.providerName = providerName;
        }else {
            weakSelf.providerId = @"";
            weakSelf.providerName = @"";
        }
        weakSelf.currentPage = 1;
        [weakSelf loadData];
    };
    [screenView showView:self];
}

-(UILabel *)numLab {
    if (!_numLab) {
        _numLab = [[UILabel alloc] init];
        _numLab.textColor = [UIColor whiteColor];
        _numLab.backgroundColor = kPinkColor;
        _numLab.font = kFont(13);
        _numLab.textAlignment = NSTextAlignmentCenter;
        _numLab.layer.cornerRadius = kScaleNum(11);
        _numLab.layer.masksToBounds = YES;
    }
    return _numLab;
}

-(UIButton *)cartBtn {
    if (!_cartBtn) {
        _cartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _cartBtn.frame = SARectMake(0, 5, 80, 45);
        [_cartBtn addTarget:self action:@selector(clickedCartBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cartBtn;
}

-(SAWarehousePurchaseFindProductPageAPI *)warehousePurchaseFindProductPageAPI {
    if (!_warehousePurchaseFindProductPageAPI) {
        _warehousePurchaseFindProductPageAPI = [SAWarehousePurchaseFindProductPageAPI apiWithDelegate:self];
    }
    return _warehousePurchaseFindProductPageAPI;
}

-(NSMutableArray<SASalesOrderViewListModel *> *)dataMarr {
    if (!_dataMarr) {
        _dataMarr = [NSMutableArray array];
    }
    return _dataMarr;
}

-(NSMutableArray<SASalesOrderViewListModel *> *)selectMarr {
    if (!_selectMarr) {
        _selectMarr = [NSMutableArray array];
    }
    return _selectMarr;
}

@end
